<?php

class FormsDAO extends CI_Model {
	
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }


    function getCountFormularios($estado=null,$tipo=null,$cod_colegio=null){
    	
    	if($estado!=null) 	$this->db->where('estado',$estado);
    	if($tipo!=null) 	$this->db->where('tipo',$tipo);
    	if($cod_colegio!=null) $this->db->where('codigo',$cod_colegio);
    	
    	return $this->db->count_all_results('header_formulario');

    }
    
    
    function getFormulariosAll($estado=null,$tipo=null,$start=0, $offset=10){
    	if($estado!=null) 	$this->db->where('estado',$estado);
    	if($tipo!=null) 	$this->db->where('tipo',$tipo);
    	$this->db->limit($offset,$start);
    	$query = $this->db->get('header_formulario');
    	return $query->result();
    }
    
    function getFormHeaderById($id_form){
		$this->db->where('id_header_formulario',$id_form);
    	$query = $this->db->get('header_formulario');
    	return $query->result();
    }

    
	function getFormVideoById($id_form){
		$this->db->where('id_header_formulario',$id_form);
    	$query = $this->db->get('videos_colegio');
    	return $query->result();
	}

	function getFormFotoById($id_form){
		$this->db->where('id_header_formulario',$id_form);
    	$query = $this->db->get('foto_colegio');
    	return $query->result();
	}
	
	function getFormDepositoById($id_form){
		$this->db->where('id_header_formulario',$id_form);
    	$query = $this->db->get('deposito');
    	return $query->result();
	}
	
	function getFormActividadById($id_form){
		$this->db->where('id_header_formulario',$id_form);
    	$query = $this->db->get('actividad_colegio');
    	return $query->result();
	}
	
	function getTareaById($id_form){
		$this->db->where('id_header_formulario',$id_form);
    	$query = $this->db->get('tarea_colegio');
    	return $query->result();
	}
	
	function getMediosByIdForm($id_form){
		$this->db->where('id_header_formulario',$id_form);
    	$query = $this->db->get('medios_upload');
    	return $query->result();
	}
	
	function updateApruebaForm($id_form){
		$this->db->where('id_header_formulario',$id_form);
		$this->db->update('header_formulario', array('estado'=>'A')); 
	}
	
	function updateRechazaForm($id_form){
		$this->db->where('id_header_formulario',$id_form);
		$this->db->update('header_formulario', array('estado'=>'C')); 
	}
	
	function updatePuntajeForm($id_form,$puntaje){
		$this->db->where('id_header_formulario',$id_form);
		$this->db->update('header_formulario', array('puntaje'=> $puntaje));
	}
	
	function refreshPuntajeTotalColegio($cod_colegio){
		
		$this->db->select_sum('puntaje');
		$this->db->where('codigo',$cod_colegio);
		$this->db->where('estado','A');
		$query = $this->db->get('header_formulario')->result();
		
		$puntaje = $query[0]->puntaje;
		
		$this->db->where('codigo',$cod_colegio);
		$this->db->update('colegio', array('puntaje_acumulado'=>($puntaje=="")?0:$puntaje)); 
		
	}
	
	function refreshRanking(){
		// refrescamos el ranking en toda la tabla
		$this->db->order_by('puntaje_acumulado','desc');
		$query = $this->db->get('colegio');
		$i=1;
		foreach ($query->result() as $item){
			$this->db->where('id_colegio',$item->id_colegio);
			$this->db->update('colegio',array('ranking'=>$i));
			$i++;
		}
		
	}	
	
    function getCountTarea($cod_colegio,$id_tarea){
    	
    	$sql="select count(*) as cuenta from tarea_colegio t join header_formulario h on h.id_header_formulario = t.id_header_formulario where h.estado = 'A' and h.codigo = '".$cod_colegio."' and t.id_tarea = ".$id_tarea;
		//echo $sql;
    	$query = $this->db->query($sql);    	
    	$result = $query->result();
    	return $result[0]->cuenta;

    }
	
    function getCountActividad($cod_colegio,$cod_actividad){
    	
    	$sql="select count(*) as cuenta from actividad_colegio a join header_formulario h on h.id_header_formulario = a.id_header_formulario where h.estado = 'A' and h.codigo = '".$cod_colegio."' and a.cod_actividad = '".$cod_actividad."'";
		//echo $sql;
    	$query = $this->db->query($sql);    	
    	$result = $query->result();
    	return $result[0]->cuenta;

    }
    
	
	
}

/* End of file colegio.php */
/* Location: ./application/models/forms/colegio.php */